package com.guigu.www.luolujin.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.luolujin.Service.LljRiskTestService;
import com.guigu.www.mapper.*;
import com.guigu.www.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 作者 罗露金
 */
@Service
public class LljRiskTestServiceImpl implements LljRiskTestService {
    @Autowired
    RiskTestMapper mapper;
    @Autowired
    RubricMapper rubricMapper;
    @Autowired
    RubricOptionMapper optionMapper;
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    UserAnswerMapper userAnswerMapper;

    @Override
    public PageVo<RiskTest> PageRiskTest(Integer pageNum, Integer pageSize, String riskTestName) {
        QueryWrapper<RiskTest> wrapper = new QueryWrapper<>();
        wrapper.like("risk_test_name  ",riskTestName);
        Page page = new Page(pageNum, pageSize);
        IPage iPage = mapper.selectPage(page, wrapper);
        PageVo<RiskTest> pageVo = new PageVo<>();
        pageVo.setRows(iPage.getRecords());
        pageVo.setTotal(iPage.getTotal());
        return pageVo;
    }

    @Override
    public List<Rubric> selectRubric(Integer rubType) {
        List<Rubric> rubrics = rubricMapper.queryRubric(rubType);
        for (Rubric rubric : rubrics) {
            QueryWrapper<RubricOption> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("rub_id",rubric.getRubId());
            List<RubricOption> rubricOptions = optionMapper.selectList(queryWrapper);
            rubric.setOptions(rubricOptions);
        }
        return rubrics;
    }

    @Override
    public void insertUserAnswer(String memId, List<UserAnswer> UserAnswer) {
        for (UserAnswer userAnswer : UserAnswer) {
            QueryWrapper<UserAnswer> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mem_id",memId);
            queryWrapper.eq("rub_id ",userAnswer.getRubId());
            userAnswerMapper.delete(queryWrapper);
            UserAnswer user = new UserAnswer();
            user.setMemId(Integer.parseInt(memId));
            user.setRubId(userAnswer.getRubId());
            user.setRubOpId(userAnswer.getRubOpId());
            userAnswerMapper.insert(user);

        }
    }

    @Override
    public void insertRiskTest(String riskTestName, Integer memId) {
        QueryWrapper<UserAnswer> userAnswerQueryWrapper = new QueryWrapper<>();
        userAnswerQueryWrapper.eq("mem_id",memId);
        List<UserAnswer> userAnswers = userAnswerMapper.selectList(userAnswerQueryWrapper);
        Integer ph=0;
        Integer qx=0;
        Integer yx=0;
        //合格分数
        float peh=0;
        float qix=0;
        float yanx=0;
        for (UserAnswer userAnswer : userAnswers) {
            //获取题目id
            Integer rubId = userAnswer.getRubId();
            //查询题库表 查询题目类型
            Rubric rubric = rubricMapper.selectById(rubId);
            Integer rubType = rubric.getRubType();
            if(rubType==19){
                //获取到及格分数
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",3);
                queryWrapper.eq("ques_test_type ",19);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                peh=quesScore;
                //获取每一个答题分数
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    ph = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    ph=ph+rubOpScore;
                }
            }else if(rubType==20){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",3);
                queryWrapper.eq("ques_test_type ",20);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                qix=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    qx = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    qx=qx+rubOpScore;
                }
            }else if(rubType==21){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",3);
                queryWrapper.eq("ques_test_type ",21);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                yanx=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null){
                    yx = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    yx=yx+rubOpScore;
                }
            }
        }
        String riskTestFood=null;
        String riskTestKeep= null;
        String riskTestSmoke= null;
        if(ph==0){
            riskTestFood="未检测";
        }else if(ph<peh){
            riskTestFood="稍差";
        }else {
            riskTestFood="良好";
        }

        if(qx==0){
            riskTestKeep="未检测";
        }else if(qx<qix){
            riskTestKeep="稍差";
        }else {
            riskTestKeep="良好";
        }

        if(yx==0){
            riskTestSmoke="未检测";
        }else if(yx<yanx){
            riskTestSmoke="稍差";
        }else {
            riskTestSmoke="良好";
        }

        QueryWrapper<RiskTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",memId);
        mapper.delete(wrapper);
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );
        RiskTest riskTest = new RiskTest();
        riskTest.setRiskTestName(riskTestName);
        riskTest.setRiskTestDatetime(time);
        riskTest.setRiskTestFood(riskTestFood);;
        riskTest.setRiskTestKeep(riskTestKeep);
        riskTest.setRiskTestSmoke(riskTestSmoke);
        riskTest.setMemId(memId);
        mapper.insert(riskTest);
    }
}
